library(ggplot2)
library(dplyr)

# Assuming 'filtered_data' and 'monthly_MFA' have the same columns
filtered_data$source <- "@P4AHCF"
monthly_MFA$source <- "@PNHP"

combined_data <- bind_rows(filtered_data, monthly_MFA)

annotation_data <- data.frame(
  x = as.Date("2020-02-01"),
  y = max(combined_data$MFA_mentions),
  label = "COVID-19"
)

ggplot(combined_data, aes(x = month, y = MFA_mentions, linetype = source, color = source)) +
  geom_line(aes(color = ifelse(source == "@P4AHCF", "black", "black"))) +
  scale_linetype_manual(values = c("@P4AHCF" = "solid", "@PNHP" = "dashed")) +
  scale_color_manual(values = c("@P4AHCF" = "black", "@PNHP" = "black")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-02-01")), linetype="dashed", color = "red", size=1.5) +
  geom_text(data = annotation_data, aes(x = x, y = y, label = label), fontface = "bold", hjust = 1.15, vjust = -18.5, inherit.aes = FALSE) + # Adjusted hjust and vjust
  scale_x_date(breaks = function(x) seq(from=min(x), to=max(x), by="6 months"), date_labels = "%b %Y") +
  labs(x = "Month", y = "Number of Mentions of 'Medicare for All'",
       title = "'Medicare for All' Mentions Over Time",
       linetype = "Source") +
  theme_minimal()

# Add source column
average_word_count$source <- "@PNHP"
average_word_count_1$source <- "@P4AHCF"

# Combine the two data frames
combined_word_count <- rbind(average_word_count, average_word_count_1)

# Create a data frame for the annotation
annotation_data <- data.frame(
  x = as.Date("2020-02-01"),
  y = max(combined_word_count$avg_word_count),
  label = "COVID-19"
)

# Create the single plot
ggplot(combined_word_count, aes(x = month, y = avg_word_count, linetype = source, color = source)) +
  geom_line() +
  scale_linetype_manual(values = c("@P4AHCF" = "solid", "@PNHP" = "dashed")) +
  scale_color_manual(values = c("@P4AHCF" = "black", "@PNHP" = "black")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-02-01")), linetype="dashed", color = "red", size=1.5) +
  geom_text(data = annotation_data, aes(x = x, y = y, label = label), fontface = "bold", hjust = 1.15, inherit.aes = FALSE) +
  scale_x_date(breaks = function(x) seq(from=min(x), to=max(x), by="6 months"), date_labels = "%b %Y") +
  labs(x = "Month", y = "Average Word Count",
       title = "Average Word Count Per Tweet Over Time",
       linetype = "Source", color = "Source") + # This ensures that both aesthetics use the same label
  guides(linetype = guide_legend(override.aes = list(color = c("black", "black")))) + # This ensures color is consistently black in the legend
  theme_minimal()

# Add source information
monthly_sums_merged$source <- "@PNHP"
monthly_sums_merged_1$source <- "@P4AHCF"

# Combine the data
combined_retweets <- rbind(
  select(monthly_sums_merged, month, sum_retweets, source),
  select(monthly_sums_merged_1, month, sum_retweets, source)
)

combined_favorites <- rbind(
  select(monthly_sums_merged, month, sum_favorites, source),
  select(monthly_sums_merged_1, month, sum_favorites, source)
)

# Create a data frame for the annotation
annotation_data_retweets <- data.frame(
  x = as.Date("2020-02-01"),
  y = max(combined_retweets$sum_retweets),
  label = "COVID-19"
)

# Create the plot for retweets
ggplot(combined_retweets, aes(x = month, y = sum_retweets, linetype = source, color = source)) +
  geom_line() +
  scale_linetype_manual(values = c("@P4AHCF" = "solid", "@PNHP" = "dashed")) +
  scale_color_manual(values = c("@P4AHCF" = "black", "@PNHP" = "black")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-02-01")), linetype="dashed", color = "red", size=1.5) +
  geom_text(data = annotation_data_retweets, aes(x = x, y = y, label = label), fontface = "bold", hjust = 1.15, inherit.aes = FALSE) +
  scale_x_date(breaks = function(x) seq(from=min(x), to=max(x), by="6 months"), date_labels = "%b %Y") +
  labs(x = "Month-Year", y = "Sum of Retweets",
       title = "Monthly Sums of Retweets Over Time",
       linetype = "Source", color = "Source") + 
  guides(linetype = guide_legend(override.aes = list(color = c("black", "black")))) +
  theme_minimal()


# Create a data frame for the annotation
annotation_data_favorites <- data.frame(
  x = as.Date("2020-02-01"),
  y = max(combined_favorites$sum_favorites),
  label = "COVID-19"
)

# Create the plot for favorites
ggplot(combined_favorites, aes(x = month, y = sum_favorites, linetype = source, color = source)) +
  geom_line() +
  scale_linetype_manual(values = c("@P4AHCF" = "solid", "@PNHP" = "dashed")) +
  scale_color_manual(values = c("@P4AHCF" = "black", "@PNHP" = "black")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-02-01")), linetype="dashed", color = "red", size=1.5) +
  geom_text(data = annotation_data_favorites, aes(x = x, y = y, label = label), fontface = "bold", hjust = 1.15, inherit.aes = FALSE) +
  scale_x_date(breaks = function(x) seq(from=min(x), to=max(x), by="6 months"), date_labels = "%b %Y") +
  labs(x = "Month-Year", y = "Sum of Favorites",
       title = "Monthly Sums of Favorites Over Time",
       linetype = "Source", color = "Source") +
  guides(linetype = guide_legend(override.aes = list(color = c("black", "black")))) +
  theme_minimal()


